<?php
class locationpicture extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('mlocation');
		$this->load->model('mlocationpicture');
	}
	
	function index($id_location)
	{
		$data['active'] = "location";
		$config['base_url'] = base_url().'locationpicture/index/'.$id_location.'/';
		$config['total_rows'] = $this->db->count_all('location_picture');
		$config['per_page'] = 10;
		$config['uri_segment'] = 4;
		
		$config['full_tag_open'] = '<div><ul class="pagination pagination-sm">';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';

		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);
		$token = $this->session->userdata('token');
		if($token){
			$offset = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
			$page = $config['per_page'];
			$data['location'] = $this->mlocation->detail($id_location);
			$data['results'] = $this->mlocationpicture->getList($id_location,$page,$offset);
			$this->load->view('locationpicture/list',$data);
		}else{
			redirect('login');
		}
	}
    
	function add($id_location){
		$data['active'] = "location";
		$data['mode'] = "Tambah";
		$data['action'] = "save";
		$data['location'] = $this->mlocation->detail($id_location);
		$this->load->view('locationpicture/form',$data);
	}
	
	function edit($id_location,$id_locationpic){
		$data['active'] = "location";
		$data['mode'] = "Ubah";
		$data['action'] = "update";
		$data['location'] = $this->mlocation->detail($id_location);
		$data['detail'] = $this->mlocationpicture->detail($id_locationpic);
		$this->load->view('locationpicture/form',$data);
	}
	
	function save()
	{
		$id_locationpic = $this->input->post('id_locationpic');
		$id_location = $this->input->post('id_location');
		$description_locationpic = $this->input->post('description_locationpic');
		
		$submit = $this->input->post('submit');	
		if($submit)
		{
			$config['upload_path'] = './upload/location';
			$config['allowed_types'] = 'jpg|png|gif';
			$this->load->library('upload', $config);
			$this->upload->do_upload('file_locationpic');
			$getFile = $this->upload->data();
			$file_locationpic = $getFile['file_name'];
			
			$this->mlocationpicture->setData($id_locationpic,$id_location,$file_locationpic,$description_locationpic);
			$this->mlocationpicture->create();
			$this->session->set_flashdata('success', true);
			redirect('locationpicture/index/'.$id_location);
		}
		$this->session->set_flashdata('error', true);
		redirect('locationpicture/index/'.$id_location);
	}
	
	function update()
	{
		$id_locationpic = $this->input->post('id_locationpic');
		$id_location = $this->input->post('id_location');
		$description_locationpic = $this->input->post('description_locationpic');
		
		$submit = $this->input->post('submit');	
		if ($submit)
		{
			if(!$this->input->post('file_locationpic')){
				$config['upload_path'] = './upload/location';
				$config['allowed_types'] = 'jpg|png|gif';
				$this->load->library('upload', $config);
				$this->upload->do_upload('file_locationpic');
				$getFile = $this->upload->data();
				$file_locationpic = $getFile['file_name'];
			}else{
				$file_locationpic = $this->input->post('file_locationpic');
			}
		
			$this->mlocationpicture->setData($id_locationpic,$id_location,$file_locationpic,$description_locationpic);
			$this->mlocationpicture->update($id_locationpic);
			$this->session->set_flashdata('success', true);
			redirect('locationpicture/index/'.$id_location);
		}
		$this->session->set_flashdata('error', true);
		redirect('locationpicture/index/'.$id_location);
	}
	
	function delete($id_location,$id_locationpic)
	{
		$getHapus = $this->mlocationpicture->detail($id_locationpic);
		
		if(@unlink("upload/location/".$getHapus[0]['file_locationpic']) || $getHapus[0]['file_locationpic'] == "0" || $getHapus[0]['file_locationpic'] == ""){
			if ($this->mlocationpicture->remove($id_locationpic)){
				$this->session->set_flashdata('delete', true);
				redirect('locationpicture/index/'.$id_location,'refresh');
			}
		}
		
		$this->session->set_flashdata('error', true);
		redirect('locationpicture/index/'.$id_location);
	}	
	
	function searchData(){
		$data['active'] = "location";
		$name = $this->input->post('name');
		$data['results'] = $this->mlocationpicture->getListSearch($name);
		$this->load->view('locationpicture/list',$data);
	}
}
?>